package com.liuj.springbucks.service.impl;

import com.liuj.springbucks.domain.Coffee;
import com.liuj.springbucks.mapper.CoffeeMapper;
import com.liuj.springbucks.service.CoffeeService;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@MapperScan("com.liuj.springbucks.mapper")
@Configuration
public class CoffeeServiceImpl implements CoffeeService {

    @Resource
    private CoffeeMapper coffeeMapper;

    @Resource
    private JedisPool jedisPool;

    @Resource
    private JedisPoolConfig jedisPoolConfig;

    @Override
    public List<Coffee> findAllWithParam(int pageNum, int pageSize) {
        return coffeeMapper.findAllWithParam(pageNum, pageSize);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void installCoffee(Coffee coffee) throws Exception {
        try {
            coffeeMapper.installCoffee(coffee);
        } catch (Exception e) {
            log.error("新增咖啡信息失败");
            throw new Exception();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCoffee(Coffee coffee) throws Exception {
        try {
            coffeeMapper.updateCoffee(coffee);
        } catch (Exception e) {
            log.error("修改咖啡信息失败");
            throw new Exception();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCoffee(Long id) throws Exception {
        try {
            coffeeMapper.deleteCoffee(id);
        } catch (Exception e) {
            log.error("删除咖啡信息失败");
            throw new Exception();
        }
    }

    @Override
    public Coffee findById(Long id) {
        return coffeeMapper.findById(id);
    }

    @Override
    public List<Coffee> findAll() {
        List<Coffee> coffeeList = coffeeMapper.findAll();
        return coffeeList;
    }

    @Bean
    @ConfigurationProperties("redis")
    public JedisPoolConfig jedisPoolConfig() {
        return new JedisPoolConfig();
    }

    @Bean(destroyMethod = "close")
    public JedisPool jedisPool(@Value("${spring.redis.host}") String host) {
        return new JedisPool(jedisPoolConfig(), host);
    }

    /**
     * 通过reid缓存咖啡名称及价格，用户根据咖啡名称便可以请求查询到咖啡的价格
     *
     * @param name
     * @return
     */
    @Override
    public String redisFind(String name) {
        log.info(jedisPoolConfig.toString());

        try (Jedis jedis = jedisPool.getResource()) {
            //循环遍历将数据库中查询到的咖啡名称及金额写入redis中
            coffeeMapper.findAll().forEach(c -> {
                jedis.hset("springbucks-menu",
                        c.getName(),
                        Long.toString(c.getPrice().getAmountMinorLong()));
            });
            //通过请求给到的咖啡名称查询咖啡的价格
            String price = jedis.hget("springbucks-menu", name);
            return price;
        }
    }
}
